package com.excellent.system.controller;

import com.excellent.common.annotation.Log;
import com.excellent.common.core.controller.BaseController;
import com.excellent.common.core.domain.AjaxResult;
import com.excellent.common.core.domain.entity.SysUser;
import com.excellent.common.core.page.TableDataInfo;
import com.excellent.common.enums.BusinessType;
import com.excellent.common.utils.ShiroUtils;
import com.excellent.common.utils.poi.ExcelUtil;
import com.excellent.system.domain.SysStuTest;
import com.excellent.system.domain.SysUserGroupInterview;
import com.excellent.system.domain.SysUserGroupMachine;
import com.excellent.system.service.ISysStuTestService;
import com.excellent.system.service.ISysUserGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 成绩输入Controller
 *
 * @author YYY
 * @date 2020-12-24
 */
@Controller
@RequestMapping("/system/test")
public class SysStuTestController extends BaseController
{
    private String prefix = "system/test";

    @Autowired
    private ISysStuTestService sysStuTestService;

    @Autowired
    private ISysUserGroupService sysUserGroupService;

    @GetMapping("/interview")
    public String testInterview(ModelMap mapp)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if(user.isAdmin()){
            mapp.put("group",0);
            startPage();
            return prefix + "/testInterview";
        }
        else{
            SysUserGroupInterview sysUserGroupInterview = sysUserGroupService.selectSysUserGroupInterviewById(user.getUserId().toString());

            if(null== sysUserGroupInterview)
            {
                mapp.put("group",-1);
            }else{
                mapp.put("group", sysUserGroupInterview.getGroupInterviewId().toString());
            }
            startPage();
            return prefix + "/testInterview";
        }

    }

    @GetMapping("/machine")
    public String testMachine(ModelMap mapp)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if(user.isAdmin()){
            mapp.put("group",0);
            startPage();
            return prefix + "/testInterview";
        }
        else{

            SysUserGroupMachine sysUserGroupMachine = sysUserGroupService.selectSysUserGroupMachineById(user.getUserId().toString());
            if(null== sysUserGroupMachine)
            {
                mapp.put("group",-1);
            }else{
                mapp.put("group", sysUserGroupMachine.getGroupMachineId().toString());
            }
            startPage();
            return prefix + "/testMachine";
        }

    }
    @GetMapping("personal")
    public String personal(ModelMap mapp)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if(user.isAdmin()){
            mapp.put("group",0);
            startPage();
            return prefix + "/test";
        }
        else{
            return prefix + "/stu";
        }

    }
    /**
     * 查询成绩输入列表
     */
    @PostMapping("/interviewList")
    @ResponseBody
    public TableDataInfo interviewList(SysStuTest sysStuTest)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if(user.isAdmin())
        {
            return  getDataTable(sysStuTestService.selectSysStuTestList(sysStuTest));
        }
        else {
            SysUserGroupInterview sysUserGroupInterview = sysUserGroupService.selectSysUserGroupInterviewById(user.getUserId().toString());
            if(sysUserGroupInterview ==null)
            {
                List<SysStuTest> list=new ArrayList<>();
                return  getDataTable(list);

            }else {
                sysStuTest.setgroupInterviewId(sysUserGroupInterview.getGroupInterviewId());
                List<SysStuTest> list1 = sysStuTestService.selectSysStuTestList(sysStuTest);
//                List<SysStuTest> list =new ArrayList<>();
//                for(SysStuTest stuTest : list1)
//                {
//                    if(stuTest.getgroupInterviewId().equals(sysUserGroupInterview.getGroupInterviewId()))
//                    {
//                        list.add(stuTest);
//                    }
//                }
                return getDataTable(list1);
            }

        }
    }
    @PostMapping("/machineList")
    @ResponseBody
    public TableDataInfo machineList(SysStuTest sysStuTest)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if(user.isAdmin())
        {
            return  getDataTable(sysStuTestService.selectSysStuTestList(sysStuTest));
        }
        else {
            SysUserGroupMachine sysUserGroupMachine = sysUserGroupService.selectSysUserGroupMachineById(user.getUserId().toString());
            List<SysStuTest> list=new ArrayList<>();
            if(sysUserGroupMachine ==null)
            {

                return  getDataTable(list);

            }else {
                list = sysStuTestService.selectSysStuTestList(sysStuTest);
                return getDataTable(list);
            }

        }
    }

    /**
     * 查询成绩输入列表
     */
    @PostMapping("/result")
    @ResponseBody
    public AjaxResult result(SysStuTest sysStuTest)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if(user.isAdmin())
        {
            List<SysStuTest>lists = sysStuTestService.selectSysStuTestList(sysStuTest);
            for (SysStuTest s:lists)
            {
                s.setComputerTest((s.getComputerTest1()+s.getComputerTest3()+s.getComputerTest2()+s.getComputerTest4()+s.getComputerTest5())/5);
                s.setMeetTest((s.getMeetTest1()+s.getMeetTest2()+s.getMeetTest3())/3);
                sysStuTestService.updateSysStuTest(s);
            }
            return  toAjax(true);
        }
        return toAjax(false);

    }
    /**
     * 查询成绩输入
     */
    @PostMapping("/one")
    @ResponseBody
    public TableDataInfo one(SysStuTest sysStuTest)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if(user.isAdmin())
        {
            return  getDataTable(sysStuTestService.selectSysStuTestList(sysStuTest));
        }
        else {
            List<SysStuTest> sysStuTestList=new ArrayList<>();
            sysStuTestList.add(sysStuTestService.selectSysStuTestById(user.getLoginName()));
            return  getDataTable(sysStuTestList);
        }
    }
    /**
     * 导出成绩输入列表
     */
    @Log(title = "成绩输入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysStuTest sysStuTest)
    {
        List<SysStuTest> list = sysStuTestService.selectSysStuTestList(sysStuTest);
        ExcelUtil<SysStuTest> util = new ExcelUtil<SysStuTest>(SysStuTest.class);
        return util.exportExcel(list, "test");
    }

    /**
     * 新增成绩输入
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存成绩输入
     */
    @Log(title = "成绩输入", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysStuTest sysStuTest)
    {
        return toAjax(sysStuTestService.insertSysStuTest(sysStuTest));
    }

    /**
     * 修改成绩输入
     */
    @GetMapping("/machineEdit/{stuSno}")
    public String machineEdit(@PathVariable("stuSno") String stuSno, ModelMap mmap)
    {
        SysUser user = ShiroUtils.getSysUser();
        if(user.isAdmin())
        {
            mmap.put("admin",1);
            mmap.put("interview",-1);
            mmap.put("computer",-1);
        }else {
            SysUserGroupMachine sysUserGroupMachine = sysUserGroupService.selectSysUserGroupMachineById(user.getUserId().toString());
            mmap.put("admin",-1);
            mmap.put("interview",-1);
            mmap.put("computer",sysUserGroupMachine.getGroupMachineId());
        }
        SysStuTest sysStuTest = sysStuTestService.selectSysStuTestById(stuSno);
        mmap.put("sysStuTest", sysStuTest);
        return prefix + "/edit";
    }
    @GetMapping("/interviewEdit/{stuSno}")
    public String interviewEdit(@PathVariable("stuSno") String stuSno, ModelMap mmap)
    {
        SysUser user = ShiroUtils.getSysUser();
        if(user.isAdmin())
        {
            mmap.put("admin",1);
            mmap.put("interview",-1);
            mmap.put("computer",-1);
        }else {
            SysUserGroupInterview sysUserGroupInterview = sysUserGroupService.selectSysUserGroupInterviewById(user.getUserId().toString());
            mmap.put("admin",-1);
            mmap.put("interview", sysUserGroupInterview.getGroupInterviewId());
            mmap.put("computer",-1);
        }
        SysStuTest sysStuTest = sysStuTestService.selectSysStuTestById(stuSno);
        mmap.put("sysStuTest", sysStuTest);
        return prefix + "/edit";
    }

    /**
     * 修改保存成绩输入
     */
    @Log(title = "成绩输入", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysStuTest sysStuTest)
    {
        return toAjax(sysStuTestService.updateSysStuTest(sysStuTest));
    }

    /**
     * 删除成绩输入
     */
    @Log(title = "成绩输入", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysStuTestService.deleteSysStuTestByIds(ids));
    }
}
